import { defineStore } from 'pinia'
import type { AxiosResponse } from 'axios'
import chinaMapJson from '@/assets/china.json'
import type { GeoJsonType } from '@/assets/types/MapTyps'
import type { DiseaseData, Children } from '@/assets/types/Disease'
import { getList } from '@/assets/utils/axios'
import axios from 'axios'
import { ref } from 'vue'
import type { TotalType } from '@/assets/types/Typs'

export const useStore = defineStore({
  id: 'region',
  state: () => ({
    // region: <Array<Object>>[],
    region: ref('china'),
    originGeoJson: JSON.parse(JSON.stringify(chinaMapJson)),
    currentGeoJson: <GeoJsonType>JSON.parse(JSON.stringify(chinaMapJson)),
    list: <GeoJsonType>{},
    // diseaseh5Shelf:<Diseaseh5Shelf>{},
    // localCityNCOVDataList:<LocalCityNCOVDataList>{}
    diseaseData: <DiseaseData>{},
    isBack: ref(false)
  }),
  getters: {
    getitem(): TotalType[]|undefined {
      if (this.diseaseData.diseaseh5Shelf===undefined) return undefined
        if (this.region === 'china') {
          let confirmAdd: number =
            this.diseaseData.diseaseh5Shelf.chinaTotal.confirmAdd
          let confirm: number =
            this.diseaseData.diseaseh5Shelf.chinaTotal.confirm
          let heal: number = this.diseaseData.diseaseh5Shelf.chinaTotal.heal
          let dead: number = this.diseaseData.diseaseh5Shelf.chinaTotal.dead
          return [
            {
              name: '中国地区',
              confirmAdd: confirmAdd,
              confirm: confirm,
              heal: heal,
              dead: dead,
            },
          ]
        } else {
          let city = <Children>{}
          this.diseaseData.diseaseh5Shelf.areaTree[0].children.some(item => {
            if (item.name === this.region) {
              city = item
            }
            return item.name === this.region
          })
          let res = city.children.map(item => {
            return {
              name: item.name,
              confirmAdd: item.today.confirm,
              confirm: item.total.confirm,
              heal: item.total.heal,
              dead: item.total.dead,
            }
          })
          return res
        }
    },
  },
  actions: {
    async getGeoJson(adcode: number) {
      const res: AxiosResponse<string, any> = await axios.get(
        `https://geo.datav.aliyun.com/areas_v3/bound/${adcode}_full.json`
      )
      this.currentGeoJson = JSON.parse(JSON.stringify(res.data))
    },
    async getAllList() {
      let result = await getList()
      this.diseaseData.diseaseh5Shelf = result.data.diseaseh5Shelf
      this.diseaseData.localCityNCOVDataList = result.data.localCityNCOVDataList
      // console.log('getAllList',result.data)
    },
  },
})
